package com.wangchao.administrator.zenmechi.ui.fragment.shopfragment;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.wangchao.administrator.zenmechi.R;
import com.wangchao.administrator.zenmechi.bean.categorybean.CategoryDataBean;
import com.wangchao.administrator.zenmechi.image.ImageHepler;
import com.wangchao.administrator.zenmechi.ui.fragment.buyFragment.BasketBean;
import com.wangchao.administrator.zenmechi.ui.fragment.buyFragment.GoodsBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UpdateListener;

/**
 * Created by Administrator on 2016/10/20.
 */
public class MyAdapterForShowList extends BaseAdapter {

    private Context context;
    private List<CategoryDataBean> list;

    public MyAdapterForShowList(Context context, List<CategoryDataBean> list) {
        this.context = context;
        this.list = list;
    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
    public View getView(final int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null){
            view = LayoutInflater.from(context).
                    inflate(R.layout.category_list_item, viewGroup, false);
            holder = new ViewHolder(view);
            view.setTag(holder);
        }else {
            holder = (ViewHolder) view.getTag();
        }

        //添加到购物车
        holder.imageCateCollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                BmobQuery<BasketBean> query = new BmobQuery<BasketBean>();
                query.addWhereNotEqualTo("goods_name",list.get(i).getName());
//                query.setLimit(50);//设置查询的条数
                query.findObjects(new FindListener<BasketBean>() {
                    @Override
                    public void done(List<BasketBean> object, BmobException e) {
                        if(e==null){
                          int count= object.get(0).getListGoods().get(0).getCounts();
                           object.get(0).getListGoods().get(0).setCounts(count++);
                            object.get(0).update(object.get(0).getObjectId(), new UpdateListener() {
                                @Override
                                public void done(BmobException e) {

                                }
                            });
                        }else{
                            List<GoodsBean> listgoods=new ArrayList<>();
                            BasketBean basket=new BasketBean();
                            GoodsBean goods=new GoodsBean();

                            goods.setGoods_name(list.get(i).getName());
                            goods.setPrice(list.get(i).getPrice()+"");
                            goods.setPic(list.get(i).getThumb());
                            goods.setCounts(1);
                            listgoods.add(goods);
                            basket.setName("bmob");
                            basket.setMarket_name(list.get(i).getMerchant_name());
                            basket.setListGoods(listgoods);
                            basket.save(new SaveListener<String>() {
                                @Override
                                public void done(String s, BmobException e) {
                                    if(e==null){
                                        Toast.makeText(context,"已添加购物车",Toast.LENGTH_SHORT).show();
                                    }
                                }
                            });
                        }
                    }
                });

            }
        });
        ImageHepler.showImage(context,list.get(i).getThumb(),holder.imageCate);
        holder.tvCateName.setText(list.get(i).getName());
        holder.tvShopName.setText(list.get(i).getMerchant_name());
        holder.tvCatePrice.setText(list.get(i).getPrice()+"");
        return view;
    }


    static class ViewHolder {
        @BindView(R.id.image_cate)
        ImageView imageCate;
        @BindView(R.id.tv_cate_name)
        TextView tvCateName;
        @BindView(R.id.tv_shop_name)
        TextView tvShopName;
        @BindView(R.id.tv_cate_price)
        TextView tvCatePrice;
        @BindView(R.id.image_cate_collect)
        ImageView imageCateCollect;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
